package angry.courses.servlets.view;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import angry.courses.database.Course;
import angry.courses.database.ICoursesDB;
import angry.courses.servlets.HtmlFormatter;

public class ViewYearServlet extends HttpServlet {
	private ICoursesDB coursesDB;
    
    public void init() throws ServletException {
	coursesDB = (ICoursesDB) getServletContext().getAttribute("coursesDB");
    }

    public void destroy() {
	coursesDB = null;
    }
    
    public void doGet(HttpServletRequest req, HttpServletResponse res)
	throws  ServletException, IOException {
    	res.setContentType("text/html");
    	PrintWriter out = res.getWriter();
    	out.println(new HtmlFormatter() {
    	    public String title() {
    		return "View Year of Study";
    	    }

       	    public void body(StringBuffer buf) {
       	    buf.append("<form method=\"post\" action=\"ViewYear\">");
       	    buf.append("<table>");
       	    
    		buf.append(tr("Please choose the year you want to view:", dropdown("year", "", "all", "1", "2", "3", "4"), button("Submit")));
    		buf.append("</table>");
    		buf.append("<br>Go " + link("back", "index.html") + " to main page.");
    	    }
    	}.htmlTag());
    	out.close();
    }
    
    public void doPost(HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException {
    	res.setContentType("text/html");
    	final int year;
    	if (req.getParameter("year").equals("all")) {
			year = 0;
		} else {
			year = Integer.parseInt(req.getParameter("year"));
		}
    	PrintWriter out = res.getWriter();
    	out.println(new HtmlFormatter() {
    	    public String title() {
    		return "View Year";
    	    }

       	    public void body(StringBuffer buf) {
       	    	if (year != 0) {
       	    		addCoursesForYear(buf, this, year);
       	    	} else {
       	    		for (int i = 1; i <= 4; i++) {
       	    			addCoursesForYear(buf, this, i);
       	    		}
       	    	}
	       	    buf.append("Go " + link("back", "index.html") + " to main page.");
	       	    buf.append("<br>" + link("View", "ViewYear") + " other years.");
       	    }   	    	
    	}.htmlTag());
    	out.close();
    }
    
    private void addCoursesForYear(StringBuffer buf, HtmlFormatter formater, int year) {
    	buf.append(formater.heading(3, "Year of study: " + year));
	    for (int i = 1; i <= 2; i++) {
    		addCoursesForSemester(buf, formater, year, i);
	    }
    }
    
    private void addCoursesForSemester(StringBuffer buf, HtmlFormatter formater, int year, int semester) {
    	List<Course> courses = coursesDB.getCourses(year, semester);
    	if (courses != null) {
	    	buf.append(formater.heading(4, "Semester " + semester));
	   	    buf.append("<table border=\"1\">");
	   	    buf.append(formater.th("Course name") + formater.th("Professor name"));
	   	    for (Course course : courses) {
	   	    	buf.append(formater.tr(300, course.name, course.prof));
	   	    }
	   	    buf.append("</table>");
	   	    buf.append("<br><br>");	   	    
    	}
    }
}
